import game from "./game";


const { ccclass, property } = cc._decorator;

@ccclass
export default class enemyCtrl_3 extends cc.Component {

    isDie = false;
    action = cc.scaleTo(4, 0, 0);
    hp = 8;
    speed


    onLoad() {
        this.init();
    }

    start() {

    }

    init() {
        this.hp = 8;
        this.isDie = false;
        this.normal()
        this.speed = 100 + Math.random() * 100;
    }

    normal() {
        let anim = this.getComponent(cc.Animation)
        anim.play("enemy_3Normal");
    }

    hit() {
        this.hp--
        if (this.hp <= 0) {
            this.isDie = true;
            this.d();
            return;
        }
        let anim = this.getComponent(cc.Animation)

        anim.play("enemy_3Hit");
    }

    hit_3Over() {
        this.normal();
    }
    //视频原创方法
    d() {
        let anim = this.getComponent(cc.Animation)
        anim.play("enemy_3Boom");
        let gg = cc.find("Canvas");
        let sc = gg.getComponent(game);
        sc.playSoundEnemyDie();

    }

    boom_3Over() {
        let gg = cc.find("Canvas");
        let sc = gg.getComponent(game);
        sc.onEnemyKilled(this.node, 3);
        sc.addScore(300);
    }
    //自己创造方法
    // die(){

    //     this.isDie = true;
    //     if(this.isDie==true){
    //        this.getComponent(cc.PolygonCollider).enabled=false;
    //        console.log("okok222");
    //     }
    //     //加载爆炸图片
    //     // cc.loader.loadRes("enemy0_die",cc.SpriteFrame,(err,res)=>{
    //     //    this.node.getComponent(cc.Sprite).spriteFrame = res;
    //     // });

    //     //加载爆炸动画
    //     let boom = this.getComponent(cc.Animation);
    //     boom.play("enemy_2Boom");
    //     this.node.runAction(this.action);
    //     //300毫秒后销毁
    //     setTimeout(() => {
    //         this.node.destroy();
    //     }, 1000);
    // }



    update(dt) {
        let gg = cc.find("Canvas");
        let sc = gg.getComponent(game);
        if (this.isDie == false) {
            if (sc.gameType == 1 || sc.gameType == 3) {
                this.node.y -= this.speed * dt;
            }

        }
        if (this.node.y < -cc.winSize.height / 2 - this.node.height / 2) {
            this.node.destroy();
            console.log("enemy3,kill");

        }
    }
}
